#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>

#include "hiir.h"
#include "hiir_codedef.h"

typedef unsigned short int U8;

int fp_ir = 0;

int ir_open(void)
{
	fp_ir = open("/dev/"HIIR_DEVICE_NAME, O_RDWR);
	if (fp_ir < 0)
	{
		 printf("ERROR:can not open %s device. read return %d\n", HIIR_DEVICE_NAME, fp_ir);
	}
	
	return fp_ir;
}

int ir_close(void)
{
	if (fp_ir > 0)
	{
		close(fp_ir);
		fp_ir = 0;
	}

	return 0;
}

int ir_read(void)
{
	int res, count;
	irkey_info_s rcv_irkey_info[1];
	unsigned long vlaue_true = 0;



	res = read(fp_ir, rcv_irkey_info, sizeof(rcv_irkey_info));
    count = res / sizeof(irkey_info_s);
    if( (res > 0) && (res<=sizeof(rcv_irkey_info)) )
    {
        if ( rcv_irkey_info[0].irkey_state_code == 1 )
        {
            vlaue_true = rcv_irkey_info[0].irkey_datal;
        }
        else
        {
            return -1;
        }
    }
    else
    {
    	printf(" Error. read irkey device error. result=%d.\n", res);
    	return -1;
    }
        

    printf("vlaue_true 0x%lx\n", vlaue_true);

	return vlaue_true;
}

int main()
{
    ir_open();
    while (1)
    {
        ir_read();
    }
    ir_close();
	return 0;
}
